<?php
class Wifi
{
	private $code;
	
	function __construct($code)
	{
		
		$this->appid = "TD0250";
		$this->md5key = "r79tYYkxfTjCySf3qZn3LE2NkHq3vRyt";
		$this->code = $code;
		$this->debug = true;
	}
	
	/**
	 * Make an HTTP request
	 *
	 * @return string API results
	 * @ignore
	 */
	function http($url, $data = array())
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		//如果是post请求
		if( is_array($data) )
		{
			curl_setopt($ch, CURLOPT_POST, 0);
			curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
		}
		$output = curl_exec($ch);
		curl_close($ch);
		return $output;
	}
	
	/**
	* 获得getAccessToken
	*/
	function getAccessToken()
	{
		if($this->debug){
			$url = "https://wifi30.51y5.net/open-sso/fa.scmd";
		}else{
			$url = "https://oauth.51y5.net/open-sso/fa.scmd";
		}
		
		$data['pid']="getAccessToken";
		$data['appId']=$this->appid;
		$data['code']=$this->code;
		$data['st'] = "m";
		$data['sign'] = md5($data['appId'].$data['code'].$data['pid'].$data['st'].$this->md5key);
		$result = json_decode($this->http($url,$data),true);
		return $result;
	}
	
	
	/**
	* 查询用户个人信息
	*/
	function qryOidUserInfo($accessToken,$oid)
	{
		if($this->debug){
			$url = "https://wifi30.51y5.net/open-sso/fa.scmd";
		}else{
			$url = "https://oauth.51y5.net/open-sso/fa.scmd";
		}
		$data['pid']="qryOidUserInfo";
		$data['appId']=$this->appid;
		$data['accessToken']=$accessToken;
		$data['oid']=$oid;
		$data['st'] = "m";
		$data['sign'] = md5($accessToken.$data['appId'].$oid.$data['pid'].$data['st'].$this->md5key);
		$result = json_decode($this->http($url,$data),true);
		return $result;
	}
}
